package com.gzpi.hwgggh.service.impl;

import com.alibaba.fastjson.JSON;
import com.gzpi.hwgggh.entity.DWC;
import com.gzpi.hwgggh.entity.DWG;
import com.gzpi.hwgggh.entity.DWTP;
import com.gzpi.hwgggh.entity.GGP;
import com.gzpi.hwgggh.mapper.DWCMapper;
import com.gzpi.hwgggh.mapper.DWTPMapper;
import com.gzpi.hwgggh.mapper.GGPMapper;
import com.gzpi.hwgggh.service.DWCService;
import com.gzpi.hwgggh.sysenum.EnumErrorCode;
import com.gzpi.hwgggh.utils.*;
import com.gzpi.hwgggh.vo.SearchConditionVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Type;
import java.util.*;

/**
 * @date : 2020-12-21 17:22
 **/
@Service
@Transactional
public class DWCServiceImpl implements DWCService {

    @Autowired
    private DWCMapper dwcMapper;

    @Autowired
    private GGPMapper ggpMapper;

    @Autowired
    private DWTPMapper dwtpMapper;

    @Override
    public Result selectDWCList(SearchConditionVo searchConditionVo) {
        List<DWC> dwcs = dwcMapper.selectDWCListBySearchVo(searchConditionVo); //获取拆除点位信息
        if (dwcs.size()>0){
            return Result.createSuccess(EnumErrorCode.SUCCESS_MESSAGE.toString("查询拆除点列表成功"),dwcs);
        }
        return Result.createError(EnumErrorCode.ERROR_OBJECT_SEARCH.toString("暂无数据"));
    }

    @Override
    public Result selectDWC(String id) {
        Map<String,Object> map = new HashMap<>(); // 条件map
        map.put("ID",id);
        DWC dwc = dwcMapper.selectDWCBandGgpCListAndDwtpByMap(map); //根据map条件获取拆除点信息
//        if (dwc==null) dwc = dwcMapper.selectDWCById(map);
        if (dwc!=null){
            List<GGP> dwcggps = dwc.getGgps(); //拆除点下 所有拆除广告牌
            if (dwcggps!=null && dwcggps.size()>0){
                //保存拼接后广告牌属性
                Map<String,Object> szwzmap = new HashMap<>();
                Map<String,Object> gyxsmap = new HashMap<>();
                Map<String,Object> ggccmap = new HashMap<>();
                Map<String,Object> zywtmap = new HashMap<>();
                //保存合并显示广告牌属性
                String szwz = "";
                String gyxs = "";
                String ggcc = "";
                String zywt = "";

                for (GGP ggp : dwcggps) {//循环拼接
                    String ggpbh = ggp.getGGBH().substring(ggp.getGGBH().indexOf("_")+1); //截取广告牌号码
                    if (StringUtils.isNotBlank(ggp.getSZWZ())) {
                        if (ggp.getSZWZ().contains("其他") && StringUtils.isNotBlank(ggp.getQTWZ())){
                            CommonTools.getMapData(szwzmap, ggp.getQTWZ(), ggpbh); //拼接号码
                        }else {
                            CommonTools.getMapData(szwzmap, ggp.getSZWZ(), ggpbh); //拼接号码
                        }
                    }
                    if (StringUtils.isNotBlank(ggp.getGYXS())) {
                        if (ggp.getGYXS().contains("其他形式（可编辑的文字）")){
                            CommonTools.getMapData(gyxsmap, "其他形式", ggpbh); //拼接号码
                        }else {
                            CommonTools.getMapData(gyxsmap, ggp.getGYXS(), ggpbh); //拼接号码
                        }
                    }
                    if (StringUtils.isNotBlank(ggp.getGGCC())) {
                        CommonTools.getMapData(ggccmap, ggp.getGGCC(), ggpbh); //拼接号码
                    }
                    if(StringUtils.isNotBlank(ggp.getZYWT())) {
                        if (ggp.getZYWT().contains("其他") && StringUtils.isNotBlank(ggp.getQTWT())){
                            CommonTools.getMapData(zywtmap, ggp.getQTWT(), ggpbh); //拼接号码
                        }else {
                            CommonTools.getMapData(zywtmap, ggp.getZYWT(), ggpbh); //拼接号码
                        }
                    }
                }

                //排序合并显示广告牌属性
                szwz = CommonTools.mapSort(szwzmap);
                gyxs = CommonTools.mapSort(gyxsmap);
                ggcc = CommonTools.mapSort(ggccmap);
                zywt = CommonTools.mapSort(zywtmap);

                Map<String, Object> ggpmap = new HashMap<>();
                ggpmap.put("szwz",szwz);
                ggpmap.put("gyxs",gyxs);
                ggpmap.put("ggcc",ggcc);
                ggpmap.put("zywt",zywt);

                dwc.setGgpmap(ggpmap);//保存需显示广告牌属性
                dwc.setIDNAME(dwc.getDWBH()+"-(C:"+dwcggps.size()+")");
                dwc.setGgps(dwcggps);
            }
            return Result.createSuccess(EnumErrorCode.SUCCESS_MESSAGE.toString("成功"),dwc);
        }else{
            return Result.createError(EnumErrorCode.ERROR_OBJECT_SEARCH.toString());
        }
    }

    @Override
    public Result updateDWC(MultipartFile file, String dwcJson, HttpServletRequest request) {
        DWC dwc = JSON.parseObject(dwcJson, DWC.class);//json数据转对象
       if (dwc!=null){
           Map<String,Object> map = new HashMap<>();//条件map
           if(file!=null){
               map.put("FID",dwc.getFID());
               map.put("ID",dwc.getID());
               DWTP dwtp = dwtpMapper.selectDWTPbyMap(map); //获取点位图片对象
               String fileName = file.getOriginalFilename().substring(0, file.getOriginalFilename().lastIndexOf("."));
               String start = fileName.substring(0, fileName.indexOf("_") + 1);
               String newFileName = start + dwc.getFID();
               if (dwtp!=null && dwtp.getCCT()!=null){
                   newFileName = dwtp.getCCT().substring(dwtp.getCCT().lastIndexOf("/")+1, dwtp.getCCT().lastIndexOf("."));
                   if(StringUtils.isNotBlank(newFileName) && !newFileName.contains("CCT_")){
                       newFileName = "CCT_"+newFileName; //设置为原图片名称
                   }
               }
               //保存图片
               FileResult fileResult = FileUtil.saveFileToPath(request, file, HwggConstants.SAVE_PATH + dwc.getFID(), HwggConstants.MAPPING_PATH + dwc.getFID(),newFileName);
               if (!fileResult.isFlag()){
                   return Result.createError(EnumErrorCode.ERROR_MESSAGE.toString("上传图片失败"));
               }
               dwtp.setCCT(fileResult.getHttpUrl());
               if (dwtpMapper.selectDWTPCountbyMap(map)>0){
                   dwtpMapper.updateDWTP(dwtp);
               }else{
                   dwtp.setID(CommonTools.getCurrentTimeMillis());
                   dwtp.setFID(dwc.getFID());
                   dwtpMapper.insertDWTP(dwtp);
               }
           }
           //保存信息
           if(dwcMapper.selectDWCcountByMap(map)>0){
                dwc.setMTIME(CommonTools.getCurrentTime());
                dwcMapper.updateDWCByDWBH(dwc);
           }else{
                dwc.setCTIME(CommonTools.getCurrentTime());
                dwc.setID(CommonTools.getCurrentTimeMillis());
                dwcMapper.insertDWC(dwc);
           }
            return Result.createSuccess(EnumErrorCode.SUCCESS_MESSAGE.toString("保存成功"));
       }
       return Result.createError(EnumErrorCode.ERROR_MESSAGE.toString("参数不能为空"));
    }

    @Override
    public Result deleteDWC(String id) {
       if (StringUtils.isNotBlank(id)){
           Map<String,Object> map = new HashMap<>();
           map.put("ID",id);
           DWC dwc = dwcMapper.selectDWCBandGgpCListAndDwtpByMap(map);
           if (dwc!=null){
               if (dwc.getGgps()!=null){
                   ggpMapper.deleteGGPByFid(dwc.getID(),"GGP_C");
               }
               dwcMapper.deleteDWCById(dwc.getID());
               return Result.createSuccess(EnumErrorCode.SUCCESS_MESSAGE.toString("删除成功"));
           }
           return Result.createError(EnumErrorCode.ERROR_OBJECT_SEARCH.toString("找不到此信息"));
       }
       return Result.createError(EnumErrorCode.ERROR_MESSAGE.toString("参数不能为空"));
    }
}
